package web.controller.address_telefon;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FilenameUtils;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import javax.faces.event.ValueChangeEvent;


import java.io.FileInputStream;
import java.io.File;

import org.richfaces.event.CurrentDateChangeEvent;

public class FileUploadBean {

	private boolean disabled;
	private boolean autoclear;
	private boolean rendered;
	private boolean required;
	private String acceptedTypes;
	private String requiredMessage;
	private String listHeight;
	private boolean immediateUpdate;
	private String listWidth;
	private String addControlLabel;
	private String clearAllControlLabel;
	private String clearControlLabel;
	private String stopEntryControlLabel;
	private String uploadControlLabel;
	private List<MyDocument> files;

	private Integer maxFilesQuantity;

	public FileUploadBean() {

		disabled = false;

		autoclear = false;

		rendered = true;

		required = false;

		immediateUpdate = false;

		acceptedTypes = "pdf";

		requiredMessage = "requiredMessage";

		listHeight = "300px";

		listWidth = "400px";

		addControlLabel = "Ekle";

		clearAllControlLabel = "Hepsini Temizle";

		clearControlLabel = "Temizle";

		stopEntryControlLabel = "Yuklemeyi Durdur";

		uploadControlLabel = "Yukle";

		maxFilesQuantity = 5;

		files = new ArrayList<MyDocument>();

	}

	public List<MyDocument> getFiles() {

		return files;

	}

	public void setFiles(List<MyDocument> files) {

		this.files = files;

	}

	public Integer getMaxFilesQuantity() {

		return maxFilesQuantity;

	}

	public void setMaxFilesQuantity(Integer maxFilesQuantity) {

		this.maxFilesQuantity = maxFilesQuantity;

	}

	public void fileUploadListener(UploadEvent event) throws IOException {

		UploadItem item = event.getUploadItem();

		/*
		 * 
		 * if you have createTempFiles context param defined with true -
		 * 
		 * you should use getFile rather than getData in your listener
		 */

		if (item != null && item.getData() != null) {

			MyDocument fileData = new MyDocument();

			fileData.setLength(item.getData().length);
			
			fileData.setName(FilenameUtils.getName(item.getFileName()));
			
			fileData.setContent(item.getData());

			files.add(fileData);

			maxFilesQuantity--;

		}

	}

	public void paint(OutputStream stream, Object object) throws IOException {

		stream.write(((MyDocument) getFiles().get((Integer) object))
				.getContent());

	}

	public Integer getSize() {

		return getFiles().size();

	}

	public String clearUploadData() {

		files.clear();

		maxFilesQuantity = 5;

		return null;

	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public boolean isAutoclear() {
		return autoclear;
	}

	public void setAutoclear(boolean autoclear) {
		this.autoclear = autoclear;
	}

	public boolean isRendered() {
		return rendered;
	}

	public void setRendered(boolean rendered) {
		this.rendered = rendered;
	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public String getAcceptedTypes() {
		return acceptedTypes;
	}

	public void setAcceptedTypes(String acceptedTypes) {
		this.acceptedTypes = acceptedTypes;
	}

	public String getRequiredMessage() {
		return requiredMessage;
	}

	public void setRequiredMessage(String requiredMessage) {
		this.requiredMessage = requiredMessage;
	}

	public String getListHeight() {
		return listHeight;
	}

	public void setListHeight(String listHeight) {
		this.listHeight = listHeight;
	}

	public boolean isImmediateUpdate() {
		return immediateUpdate;
	}

	public void setImmediateUpdate(boolean immediateUpdate) {
		this.immediateUpdate = immediateUpdate;
	}

	public String getListWidth() {
		return listWidth;
	}

	public void setListWidth(String listWidth) {
		this.listWidth = listWidth;
	}

	public String getAddControlLabel() {
		return addControlLabel;
	}

	public void setAddControlLabel(String addControlLabel) {
		this.addControlLabel = addControlLabel;
	}

	public String getClearAllControlLabel() {
		return clearAllControlLabel;
	}

	public void setClearAllControlLabel(String clearAllControlLabel) {
		this.clearAllControlLabel = clearAllControlLabel;
	}

	public String getClearControlLabel() {
		return clearControlLabel;
	}

	public void setClearControlLabel(String clearControlLabel) {
		this.clearControlLabel = clearControlLabel;
	}

	public String getStopEntryControlLabel() {
		return stopEntryControlLabel;
	}

	public void setStopEntryControlLabel(String stopEntryControlLabel) {
		this.stopEntryControlLabel = stopEntryControlLabel;
	}

	public String getUploadControlLabel() {
		return uploadControlLabel;
	}

	public void setUploadControlLabel(String uploadControlLabel) {
		this.uploadControlLabel = uploadControlLabel;
	}

	/* Getters - setters */

}

/*public class FileUploadBean{
    
    private ArrayList<File> files = new ArrayList<File>();
    private int uploadsAvailable = 5;
    private boolean autoUpload = false;
    private boolean useFlash = false;
    public int getSize() {
        if (getFiles().size()>0){
            return getFiles().size();
        }else 
        {
            return 0;
        }
    }

    public FileUploadBean() {
    }

    public void paint(OutputStream stream, Object object) throws IOException {
        stream.write(getFiles().get((Integer)object).getData());
    }
    public void listener(UploadEvent event) throws Exception{
        UploadItem item = event.getUploadItem();
        File file = new File();
        file.setLength(item.getData().length);
        file.setName(item.getFileName());
        file.setData(item.getData());
        files.add(file);
        uploadsAvailable--;
    }  
      
    public String clearUploadData() {
        files.clear();
        setUploadsAvailable(5);
        return null;
    }
    
    public long getTimeStamp(){
        return System.currentTimeMillis();
    }
    
    public ArrayList<File> getFiles() {
        return files;
    }

    public void setFiles(ArrayList<File> files) { 
        this.files = files;
    }

    public int getUploadsAvailable() {
        return uploadsAvailable;
    }

    public void setUploadsAvailable(int uploadsAvailable) {
        this.uploadsAvailable = uploadsAvailable;
    }

    public boolean isAutoUpload() {
        return autoUpload;
    }

    public void setAutoUpload(boolean autoUpload) {
        this.autoUpload = autoUpload;
    }

    public boolean isUseFlash() {
        return useFlash;
    }

    public void setUseFlash(boolean useFlash) {
        this.useFlash = useFlash;
    }

}*/
